package com.hanbang.taitaigou.ui.activity.aboutme.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;

import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by fanxin on 2016/3/17.
 */
public class AboutMeSetUpActivity extends BaseActivity{
    /**提交按钮**/
    private TextView button;
    /**联系客服**/
    private LinearLayout ll_contact_customer_service;
    /**关于我们**/
    private LinearLayout ll_set_up_about_us;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aboutme_set_up);
        initView();
        setOnClickListener();

//        IntentFilter intentFilter = new IntentFilter();
//        intentFilter.addAction("tuichu");
//        registerReceiver(broadcastReceiver,intentFilter);
    }

//    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            button.setText("退出登录");
//        }
//
//    };
    //初始化
    private void initView() {
        // 设置顶部的标题
        setTop(null, 0, getString(R.string.top_set_up), null, 0);
        setBackArrows(findViewById(R.id.top_bar_left_iv));
        button = (TextView)findViewById(R.id.button);

        ll_contact_customer_service = (LinearLayout)findViewById(R.id.ll_contact_customer_service);
        ll_set_up_about_us = (LinearLayout)findViewById(R.id.ll_set_up_about_us);

    }
    //设置点击事件
    private void setOnClickListener(){
        button.setOnClickListener(onClickListener);
        ll_contact_customer_service.setOnClickListener(onClickListener);
        ll_set_up_about_us.setOnClickListener(onClickListener);
    }

    //点击事件
    @SuppressWarnings("unused")
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        Intent intent = new Intent();
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                //退出登入(已修改)
                case R.id.button:
                    if (isLogin(true)) {
                        passwordIsError(true);
                    }
                    isLoginChangUi();

                    break;
                //联系客服
                case R.id.ll_contact_customer_service:
                    intent.setClass(AboutMeSetUpActivity.this,ContactCustomerServiceActivity.class);
                    startActivity(intent);
                    break;
                //关于我们
                case R.id.ll_set_up_about_us:
                    intent.setClass(AboutMeSetUpActivity.this,AboutMeSetUpAboutUsActivity.class);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    };

    //退出登入清空主页保存的地址
    private void exitLogin(){
        File file = new File("data/data/com.hanbang.taitaigou/info.txt");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(file);
            fos.write(("").getBytes());
            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

//        File f = new File("data/data/com.hanbang.taitaigou/info.txt");
//        if (f.exists()) f.delete();
    }
    private void isLoginChangUi() {
        /**
         * 判断是登录还是未登录
         */
        if (isLogin(false)) {
            button.setText("退出登录");
        } else {
            button.setText("立即登录");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        isLoginChangUi();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unregisterReceiver(broadcastReceiver);
    }
}
